package com.stx.stcp.service.impl;

import com.github.pagehelper.PageHelper;
import com.stx.stcp.bean.MenuBean;
import com.stx.stcp.mapper.MenuMapper;
import com.stx.stcp.service.MenuService;
import com.stx.stcp.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public void insert(HttpServletRequest request, MenuBean menuBean) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime=sdf.format(new Date());
        String img = FileUtil.upload(menuBean.getMyfile(),request);
        menuBean.setImg(img);
        menuBean.setCreateTime(createTime);
        menuMapper.insert(menuBean);
    }

    @Override
    public List menuList(int pageNum) {
        PageHelper.startPage(pageNum,9);
        List list=menuMapper.menuList();
        return list;
    }

    @Override
    public MenuBean selectById(int id) {
        MenuBean menuBean = menuMapper.selectById(id);
        return menuBean;
    }

    @Override
    public void update(MenuBean menuBean,HttpServletRequest request) {
        if(menuBean.getMyfile().getSize()>0){
            String img=FileUtil.upload(menuBean.getMyfile(),request);
            menuBean.setImg(img);
        }
        menuMapper.update(menuBean);
    }

}
